﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.SaaS.CustomerManage
{
    public partial class MessageAdd : BasePage
    {
        #region 属性

        public Guid MessageID
        {
            get
            {
                if (ViewState["MsgID"] == null)
                    return Guid.Empty;
                else
                    return new Guid(ViewState["MsgID"].ToString());
            }
            set { ViewState["MsgID"] = value; }
        } 

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitForm();

                if (!string.IsNullOrEmpty(Request.QueryString["MsgID"]))
                {
                    MessageID = new Guid(Request.QueryString["MsgID"]);
                    SetFormData();

                }
                else
                { 
                    MessageID = Guid.NewGuid();
                }
            }
        }

        private void InitForm()
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BizCustomerQABll bll = new BizCustomerQABll();
            BizCustomerQAEntity entity = null;

            entity = new BizCustomerQAEntity();
            entity.MessageID = MessageID;
            entity.Answer = txtAnswer.Text;
            entity.AnswerBy = base.User.Identity.LoginUser.UserID;
            entity.AnswerByName = base.User.Identity.LoginUser.UserName;
            entity.CreatedAt = DateTime.Now;
            entity.AnswerAt = DateTime.Now;
            entity.LastModifyByName = base.User.Identity.LoginUser.UserName;
            entity.LastModifyAt = DateTime.Now;
            entity.Deleted = false;

           
                bll.ReplyCustomerMessageAnswer(entity);
           

            Response.RedirectPermanent("MessageList.aspx");
        }

        private void SetFormData()
        {
            if (MessageID != Guid.Empty)
            {
                BizCustomerQAEntity entity = null;
                CustomerBll bll = new CustomerBll();
                entity = bll.Get<BizCustomerQAEntity>(MessageID);

                ltlCreatedAt.Text = entity.CreatedAt.ToString();
                ltlQuestion.Text = entity.Question;
                ltlAnswerBy.Text = entity.AnswerByName;
                ltlAnswerAt.Text = entity.AnswerAt == DateTime.MinValue ? "" : entity.AnswerAt.ToString();
                ltlLastModifyBy.Text = entity.LastModifyByName;
                ltlLastModifyAt.Text = entity.LastModifyAt == DateTime.MinValue ? "" : entity.LastModifyAt.ToString();
                txtAnswer.Text = entity.Answer;

                if (entity.CustomerID != Guid.Empty)
                {
                    BizCustomerEntity cusEntity = bll.Get<BizCustomerEntity>(entity.CustomerID);
                    if (cusEntity != null)
                    {
                        ltlCustomerName.Text = cusEntity.CustomerName;
                        ltlCustomerCode.Text = cusEntity.CustomerCode;
                    }
                }
            }
        }

    }
}